package com.coderside.fragment.arithmetic;

import java.util.ArrayList;

import com.sun.org.apache.bcel.internal.generic.IFNONNULL;

public class Sunday {
	public Sunday() {
		//
	}

	public ArrayList QfindChr(String str, String Sfind) {
		int str_length = 0;
		int fin_length = 0;

		int find_count = 0;
		int start = 0;
		int moveNum = 0;
		ArrayList<Integer> index = new ArrayList<Integer>();

		if (Sfind.length() == 0 || str.length() == 0) {
			return null;
		}

		if (str.length() < Sfind.length()) {
			return null;
		}

		str_length = str.length();
		fin_length = Sfind.length();

		while (start + fin_length <= str_length) {
			moveNum++;
			boolean isfind = false;// 是否在这次移动中找到
			String s_temp = str.substring(start, start + fin_length);
			if (Sfind.equals(s_temp)) {
				index.add(start);// 记录匹配字符的位置的第一个字符坐标
				find_count++;
				start = start + fin_length;
				isfind = true;
			}
			if (isfind == false)// 如果没找到计算下次移动位置
			{
				int forwardPos = QfindPos(str, Sfind, start, fin_length);
				start = forwardPos;
			}
		}
		System.out.println("move_count = " + moveNum);// 移动次数显著减少
		return index;
	}

	// 找字符在字符串(不算最后一个字符)的位置(倒数)
	// 没找到返回fin_length,找到返回位置
	// / 找字符在字符串(不算最后一个字符)的位置(倒数);没找到返回str.length,找到返回位置
	public int QfindPos(String str, String find, int pos, int fin_length) {
		int returnPos = str.length();
		char[] Schr = str.toCharArray();
		char[] Sfin = find.toCharArray();

		if ((pos + fin_length) < str.length()) {
			char chrFind = Schr[pos + fin_length];// 要找的字符
			if (fin_length >= 1) {
				if (find.lastIndexOf(chrFind) > -1)// 如果find里存在chrFind字符
				{
					returnPos = pos + fin_length - find.lastIndexOf(chrFind);
				} else {// 如果find里不存在chrFind字符
					returnPos = pos + fin_length + 1;
				}
			}
		}
		return returnPos;
	}

	public static void main(String[] args) {
		String str = "return1111chrFind1Pos = chrFind1 pos + fin_length -returnPos = pos + fin_length -returnPos"
				+ " = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos "
				+ "+ fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length"
				+ " -returnPos = pos + fin_length -returnPos = pos + fin_length - find.lastIndexOf(chrFind);returnPos = pos "
				+ "+ fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length "
				+ "-returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos"
				+ " + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -"
				+ " find.lastIndexOf(chrFind);returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos +"
				+ " fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length "
				+ "-returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos + fin_length -returnPos = pos "
				+ "+ fin_length -returnPos = pos + fin_length - find.lastIndexOf(chrFind1)";
		String find = "chrFind13";
		Sunday sunday = new Sunday();

		ArrayList<Integer> index = sunday.QfindChr(str, find);
		if (index == null) {
			System.out.println("条件不符合");
			return;
		}
		System.out.println("count = " + index.size());
		for (int i = 0; i < index.size(); i++) {
			System.out.print("index:" + index.get(i) + ", ");
		}
	}
}
